light=string.char(0,100,0)
dark=string.char(0,0,0)
array={
light..light..dark..light..light..light..light,
dark..dark..dark..dark..dark..light..light,
light..light..light..dark..light..light..dark,
light..dark..light..dark..light..light..light,
dark..dark..light..light..dark..light..light,
light..dark..light..light..light..dark..light,
light..light..light..light..light..dark..light,
dark..dark..dark..dark..light..light..light,
light..light..light..light..light..light..light,
light..dark..light..light..light..light..light
}

ws2812.init()

ws2812.write(array[1])

wifi.setmode(wifi.STATION)

if (wifi.sta.sethostname("NodeMCU-CLOCK") == true) then
    print("hostname was successfully changed")
else
    print("hostname was not changed")
end

station_cfg={}
station_cfg.ssid="Xiaomi_S00"
station_cfg.pwd="shuang00"
station_cfg.save=true
wifi.sta.config(station_cfg)

dateutil = require("dateutil")

rsec, rusec = rtctime.get()
print('rtc', rsec, rusec)

ws2812.write(array[2])

cli = net.createConnection(net.TCP, 0)


synctimer = tmr.create()
synctimer:register(5000, tmr.ALARM_AUTO, 
    function (t) 
       if (wifi.sta.getip() ~= nil) then
            print("Config done, IP is " .. wifi.sta.getip())
            cli:dns(dateutil.sync_server, 
                function(conn, ip) 
                    sntp.sync(ip,
                      function(sec, usec, server, info)
                        print('sync', sec, usec, server)
                        synctimer:interval(60000)
                      end,
                      function()
                       print('failed!')
                      end
                   )
                end)
            
            
            
        end
    end
)
synctimer:start()


dot=true
updatetimer = tmr.create()
updatetimer:register(1000, tmr.ALARM_AUTO, 
    function (t) 
        rsec, rusec = rtctime.get()
        year, month, date, hour, minite, secend = dateutil.analyse(rsec, 8)

        print("date",year, month, date, hour, minite, secend)

        d1=minite%10
        d2=minite/10
        d3=hour%10
        d4=hour/10

        print("update number",d1,d2,d3,d4)

        if(dot) then
            dot=false
            ws2812.write(array[d1+1]..array[d2+1]..light..light..array[d3+1]..array[d4+1])
        else  
            dot=true
            ws2812.write(array[d1+1]..array[d2+1]..dark..dark..array[d3+1]..array[d4+1])
        end
    end
)
updatetimer:start()



